package com.ys.filelib.filedialog.ftp;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class FTPService {

    final Map<String, FTPDriveHelper> mHelpers = new HashMap<>();
    private static FTPService mService;

    public static FTPService getService() {
        if (mService == null) {
            mService = new FTPService();
        }
        return mService;
    }

    public Collection<FTPDriveHelper> getHelpers() {
        return mHelpers.values();
    }

    public static void setFTPHelper(FTPHelper helper) {
        String host = helper.getHost();
        int port = helper.getPort();
        FTPService fs = getService();
        FTPDriveHelper fh = getFTPHelper(host, port, helper.getUserName());
        if (fh != null) {
            fh.setFtpHelper(helper);
        } else {
            fs.mHelpers.put(host + port + helper.getUserName(), new FTPDriveHelper(helper));
        }
    }

    public static FTPDriveHelper getFTPHelper(String host, int port, String name) {
        return getService().mHelpers.get(host + port + name);
    }
}
